package com.javahu.xiuxian.controller;


import com.javahu.xiuxian.entity.User;
import com.javahu.xiuxian.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    private final UserService userService;
    public UserController(UserService userService) {
        this.userService = userService;
    }

    // 显示用户列表
    @GetMapping("/list")
    public String list(Model model) {
        List<User> users = userService.list();
        model.addAttribute("users", users);
        return "user-list"; // 对应 templates/user-list.html
    }

    // 新增用户
    @PostMapping("/add")
    @ResponseBody
    public User addUser(@RequestParam String username) {
        User u = new User();
        u.setUsername(username);
        u.setLevel(1);
        userService.save(u);
        return u;
    }
}

